package com.lbn.jfqsc.app.message.action.discuss;

import android.text.TextUtils;

import com.lbn.baseframe.mvp.BasePresenter;
import com.lbn.baseframe.net.business.RxSubscribe;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.business.bean.JobList;
import com.lbn.jfqsc.net.Host;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李炳南
 * @date 2017/3/3
 */
public class DiscussPresenter extends BasePresenter<IDiscussView> {

    public void comment(String actId, String content) {

        if (TextUtils.isEmpty(actId)) {
            getView().InvalidData();
            return;
        }
        if (TextUtils.isEmpty(content)) {
            getView().InvalidData();
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("userId", App.getUser().getId() + "");
        map.put("actId", actId);
        map.put("content", content);

        subscribeNetwork(Host.getApi().comment(map), new RxSubscribe() {
            @Override
            public void next(Object o) {
                getView().commentSuccess();
            }

            @Override
            public void error(Throwable e) {
                getView().onError(e.getMessage());
            }
        });
    }

    public void commentList(String actId, final int page) {

        subscribeNetwork(Host.getApi().commentList(Integer.valueOf(actId), page), new RxSubscribe<List<JobList>>() {
            @Override
            public void next(List<JobList> o) {
                if (page == 1) {
                    getView().setData(o);
                } else {
                    getView().addData(o);
                }

                if (o.size() < 10) {
                    getView().isMore(false);
                } else {
                    getView().isMore(true);
                }
            }

            @Override
            public void error(Throwable e) {
                getView().onError(e.getMessage());
            }
        });


    }

}
